#include "MainMenuManager.h"
#include "MainMenu.h"
#include "Logger.h"
#include "Exception.h"

#include <boost/bind.hpp>

using namespace game_engine;
using namespace game_engine::gui;

///////////////////////////////////////////////////////////////////////////////
// Constructors and Destructors
///////////////////////////////////////////////////////////////////////////////

MainMenuManager::MainMenuManager(void)
    : SceneManager()
{
    _btnConnections.clear();
}

MainMenuManager::MainMenuManager(MainMenu *parent)
    : SceneManager(parent)
{
    _btnConnections.clear();
}

MainMenuManager::~MainMenuManager(void)
{
}

///////////////////////////////////////////////////////////////////////////////
// Other public methods
///////////////////////////////////////////////////////////////////////////////

void MainMenuManager::initialize(void) throw(...)
{
    // This is where all connections to signals are defined
    PushButton *play = dynamic_cast<MainMenu*> (scene)->btnPlay();
    PushButton *options = dynamic_cast<MainMenu*> (scene)->btnOptions();
    PushButton *exit = dynamic_cast<MainMenu*> (scene)->btnExit();

    boost::signals2::connection c;
    c = play->connect(play->signalClicked(), boost::bind(&MainMenuManager::onPlay, this, _1));
    _btnConnections.push_back(c);

    c = options->connect(options->signalClicked(), boost::bind(&MainMenuManager::onOptions, this, _1));
    _btnConnections.push_back(c);

    c = exit->connect(exit->signalClicked(), boost::bind(&MainMenuManager::onExit, this, _1));
    _btnConnections.push_back(c);
}

const BOOL MainMenuManager::update(void) throw(...)
{
    return true;
}

void MainMenuManager::finalize(void) throw(...)
{
    // This is where all connections to signals are destroyed
    vector<boost::signals2::connection>::iterator it;
    for (it=_btnConnections.begin(); it<_btnConnections.end(); it++)
    {
        (*it).disconnect();
    }

    _btnConnections.clear();
}

///////////////////////////////////////////////////////////////////////////////
// Slots
///////////////////////////////////////////////////////////////////////////////

void MainMenuManager::onPlay(BOOL clicked)
{
    if (clicked)
    {
        setMenuFocus(dynamic_cast<MainMenu*> (scene)->btnPlay());
    }
}

void MainMenuManager::onOptions(BOOL clicked)
{
    if (clicked)
    {
        setMenuFocus(dynamic_cast<MainMenu*> (scene)->btnOptions());
    }
}

void MainMenuManager::onExit(BOOL clicked)
{
    if (clicked)
    {
        setMenuFocus(dynamic_cast<MainMenu*> (scene)->btnExit());
    }
}

///////////////////////////////////////////////////////////////////////////////
// Private methods
///////////////////////////////////////////////////////////////////////////////

void MainMenuManager::setMenuFocus(PushButton *btn)
{
    vector<Widget*>widgets = dynamic_cast<MainMenu*> (scene)->widgets();
    vector<Widget*>::const_iterator it;
    for (it=widgets.begin(); it<widgets.end(); it++)
    {
        if (btn == dynamic_cast<PushButton*>(*it))
        {
            dynamic_cast<PushButton*>(*it)->setFocus(true);
        }
        else
        {
            dynamic_cast<PushButton*>(*it)->setFocus(false);
        }
    }
}
